package com.sisgh.jefdv1.lang;

/**
 * 格式化 StringBuilder
 */
public class FormatStringBuilder {

    private static final String SPACE = " ";
    private static final String TAB = "\t";
    private static final String NEWLINE = "\n";

    private final String end;
    private final StringBuilder sb;

    public FormatStringBuilder() {
        this("");
    }

    public FormatStringBuilder(String end) {
        sb = new StringBuilder();
        this.end = end;
    }

    public FormatStringBuilder append(String s) {
        sb.append(s);
        return this;
    }

    public FormatStringBuilder append(char c) {
        sb.append(c);
        return this;
    }

    public FormatStringBuilder append(Object o) {
        sb.append(o);
        return this;
    }

    public FormatStringBuilder appendWithSpace(String s) {
        sb.append(s).append(SPACE);
        return this;
    }

    public FormatStringBuilder appendWithNewline(String s) {
        sb.append(s).append(NEWLINE);
        return this;
    }

    public FormatStringBuilder newLine() {
        sb.append(NEWLINE);
        return this;
    }

    public FormatStringBuilder space() {
        sb.append(SPACE);
        return this;
    }

    public FormatStringBuilder end() {
        sb.append(end);
        return this;
    }

    public FormatStringBuilder endWithNewline() {
        sb.append(end).append(NEWLINE);
        return this;
    }

    public FormatStringBuilder deleteCharAt(int pos) {
        sb.deleteCharAt(pos);
        return this;
    }

    public FormatStringBuilder deleteLastChars(int cnt) {
        sb.delete(sb.length() - cnt, sb.length());
        return this;
    }

    public FormatStringBuilder deleteLastChar() {
        return deleteLastChars(1);
    }

    public FormatStringBuilder delete(int start, int end) {
        sb.delete(start, end);
        return this;
    }

    public FormatStringBuilder appendWithPrefixTab(String s) {
        sb.append(TAB).append(s);
        return this;
    }

    public FormatStringBuilder tab() {
        sb.append(TAB);
        return this;
    }

    public FormatStringBuilder tab(int n) {
        while (n-- > 0) {
            sb.append(TAB);
        }
        return this;
    }

    public FormatStringBuilder levelTagWithSpace(String level) {
        sb.append("[").append(level).append("]").append(SPACE);
        return this;
    }

    @Override
    public String toString() {
        return sb.toString();
    }
}
